package erfgame.core.world.terrain.file;

import java.io.File;

import erfgame.core.dao.ObjectDAO;

public class GenericFileObjectDAO<KeyType, ObjectType> extends BaseFileObjectDAO<ObjectType> implements
		ObjectDAO<KeyType, ObjectType> {

	private String extension;
	private File directory;
	
	public GenericFileObjectDAO( FileStreamFactory streamFactory, Serializer<ObjectType> serializer, File directory, String extension ) {
		super( serializer, streamFactory );
		this.directory = directory;
		this.extension = extension;
	}
	
	public boolean containsObject(KeyType key) {
		return getFile( key ).exists();
	}

	public boolean removeObject(KeyType key) {
		File file = getFile( key );
		if( file.exists() ) {
			file.delete();
			return true;
		} else {
			return false;
		}
	}

	public ObjectType retrieveObject(KeyType key) {
		return this.retrieveObject( getFile( key ), null );
	}

	public void storeObject(KeyType key, ObjectType object) {
		this.storeObject( getFile( key ), object );
	}
	
	public File getFile( KeyType key ) {
		return new File( this.directory, key.toString() + "." + extension );
	}
}
